/*
   Copyright 2009 Bégaudeau Stéphane

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package faneclipse.builder;

import java.util.Map;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

/**
 * The builder of a project.
 * This class will visit every file of the project and check if they are valid.
 * @author BEGAUDEAU stephane
 */
public class FanBuilder extends IncrementalProjectBuilder {
	/**
	 * The ID of the builder.
	 */
	public static final String BUILDER_ID = "FanEclipse.fanBuilder";
	
	/**
	 * The constructor.
	 */
	public FanBuilder() {
		super();
	}
	
	/**
	 * @param kind The kind of build.
	 * @param arg1 The arguments.
	 * @param monitor The progress monitor.
	 * @throws CoreException Throw a CoreException if the build goes wrong.
	 * @return The project that we are building.
	 * @see org.eclipse.core.resources.IncrementalProjectBuilder#build(int, java.util.Map, org.eclipse.core.runtime.IProgressMonitor)
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected final IProject[] build(final int kind, final Map arg1, final IProgressMonitor monitor) throws CoreException {
		System.out.println("Build");
		if (kind == FULL_BUILD)
		{
			fullBuild(monitor);
		}
		else
		{
			IResourceDelta delta = getDelta(getProject());
			if (delta == null)
			{
				fullBuild(monitor);
			} 
			else
			{
				incrementalBuild(delta, monitor);
			}
		}
		return null;
	}
	
	/**
	 * @param monitor The progress monitor.
	 * @throws CoreException If the project has a problem with the FanResourceVisitor.
	 */
	protected final void fullBuild(final IProgressMonitor monitor) throws CoreException {
		getProject().accept(new FanResourceVisitor());
	}
	
	/**
	 * @param delta The IResourceDelta.
	 * @param monitor the progress monitor.
	 * @throws CoreException If the project has a problem with the FanDeltaVisitor.
	 */
	protected final void incrementalBuild(final IResourceDelta delta, final IProgressMonitor monitor) throws CoreException {
		delta.accept(new FanDeltaVisitor());
	}
}
